﻿var token = localStorage.getItem("token");
$(document).ready(function () {
    ShowTime();
});
var datalists = [];
var Datetimenow = new Date();
var year = Datetimenow.getFullYear() + '-' + (Datetimenow.getMonth() + 1) + '-' + Datetimenow.getDate() + ' ' + Datetimenow.getHours() + ':' + Datetimenow.getMinutes() + ':' + Datetimenow.getSeconds();

function ShowTime() {
    var ids = [];
    var cheddata = [];
    // 表单提交事件

    layui.use('table', function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.$;
        var util = layui.util;
        var form = layui.form;
        //渲染表格的函数
        function renderTable(pageNum) {
            //获取查询条件
            var ClientCode = $("#ClientCode").val();
            var ClientName = $("#ClientName").val();
            var Landline = $("#Landline").val();
            let showclient = {
                ClientCode: ClientCode,
                ClientName: ClientName,
                Landline: Landline,
                PageIndex: pageNum,
                PageSize: 100,
                Token: token
            };
            //发送AJAX请求
            $.ajax({
                url: '/Client/GetClientShow',
                type: 'get',
                data: showclient,
                dataType: 'json',
                success: function (res) {
                    var data = JSON.parse(res)
                    console.log(data)
                    //渲染表格
                    table.render({
                        elem: '#dataTable',
                        cols: [[
                            { type: 'checkbox', fixed: 'left' }, // 启用多选框
                            { field: 'clientCode', title: '客户编号' },
                            { field: 'clientName', title: '客户名称' },
                            { field: 'landline', title: '电话' },
                            { field: 'cfax', title: '传真' },
                            { field: 'mailbox', title: 'Email' },
                            { field: 'ccontact', title: '联系人' },
                            { field: 'caddress', title: '地址' },
                            {
                                field: 'creatTime', title: '创建时间', templet: function (d) {
                                    let dateobj = new Date(d.creatTime);
                                    let dateyear = dateobj.getFullYear();
                                    let datemonth = ('0' + (dateobj.getMonth() + 1)).slice(-2);
                                    let dateday = ('0' + dateobj.getDate()).slice(-2);
                                    let datehours = ('0' + (dateobj.getHours())).slice(-2);
                                    let dateminutes = ('0' + (dateobj.getMinutes())).slice(-2);
                                    let dateseconds = ('0' + (dateobj.getSeconds())).slice(-2);
                                    return dateyear + '-' + datemonth + '-' + dateday + ' ' + datehours + ':' + dateminutes + ':' + dateseconds;
                                }
                            },
                            {
                                title: '操作',
                                templet: function (d) {
                                    //编辑
                                    return ' <button type="button" onclick="editItem(' + d.clientId + ')" class="layui-btn layui-btn-xs">编辑</button>' + ' <button type="button" onclick="deleteItem(' + d.clientId + ')" class="layui-btn layui-btn-xs">删除</button>'
                                }
                            },
                        ]],
                        data: data.data.list,//数据源
                        page: true,
                        limit: 10,
                        limits: [10, 20, 30],
                        curr: pageNum,
                        total: data.data.lcount,  // 总记录数
                        loading: true, // 启用加载状态
                        toolbar: "#toolbarDemo",
                        defaultToolbar: ["exports"]
                    });
                },
                Error: function (xhr, status, error) {
                    console.log("请求失败" + error);
                }
            });
        }
        // 初次加载表格数据
        renderTable(1);
        // 查询按钮点击事件
        $('#searchBtnClient').click(function () {
            renderTable(1);  // 重置为第一页，并重新加载数据
        });
        // 触发表格复选框选择
        table.on('checkbox(dataTableFilter)', function (obj) {
            cheddata = table.checkStatus('dataTable').data;
            ids = cheddata.map(function (item) {
                return item.clientId;
            })
            console.log(ids);
        });
        $('#BathClientDel').click(function () {
            if (ids.length == 0) {
                layer.msg('请先选择要删除的项', { icon: 2 });
                return;
            }
            // 询问是否删除
            layer.confirm('确定删除吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                let delclient = {};
                delclient.ClientId = ids;
                delclient.DelName = "呓语";
                delclient.DelTime = year;
                delclient.Token = token;
                //执行删除操作
                $.ajax({
                    url: '/Client/DelAllClient',
                    type: 'post',
                    data: delclient,
                    success: function (res) {
                        var s = JSON.parse(res)
                        if (s.data > 0) {
                            layer.msg('删除成功', { icon: 1 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('删除失败');
                            return
                        }
                    }
                })
            }, function () {
                layer.msg('已取消', {
                    time: 20000, //20s后自动关闭
                    btn: ['确定']
                });
            });
        });

        //刷新
        $("#Clientrefresh").click(function () {
            location.reload()
        })

        ///////////////////////////////////编辑/////////////////
        window.editItem = function (clientId) {
            let selectedRows = [];
            //可以弹出编辑框或跳转到编辑页面
            let obj = {};
            let fclient = {
                ClientId: clientId,
                Token: token
            }
            let showcuscontact = {
                Token: token
            };
            $.ajax({
                url: '/Client/GetClientFt',
                type: 'get',
                data: fclient,
                success: function (res) {
                    var data = JSON.parse(res);
                    console.log(data);
                    $(data.data).each(function () {
                        $("#ClientId").val(this.clientId);
                        $("#UpdClientCode").val(this.clientCode);
                        $("#UpdClientName").val(this.clientName);
                        $("#UpdCfax").val(this.cfax);
                        $("#UpdLandline").val(this.landline);
                        $("#UpdMailbox").val(this.mailbox);
                        $("#UpdCremark").val(this.cremark);
                        $("#CusContactId").val(this.cusContactId);
                        $("#Office_Id").val(this.office_Id);
                        $("#User_Id").val(this.user_Id);
                        $("#AddName").val(this.addName);
                        $("#AddTime").val(this.addTime);
                        $("#UpdName").val(this.updName);
                        $("#UpdTime").val(this.updTime);

                        obj.ClientId = this.clientId;
                        obj.ClientCode = this.clientCode;
                        obj.ClientName = this.clientName;
                        obj.Cfax = this.cfax;
                        obj.Landline = this.landline;
                        obj.Mailbox = this.mailbox;
                        obj.Cremark = this.cremark;
                        obj.CusContactId = this.cusContactId;
                        obj.Office_Id = this.office_Id;
                        obj.User_Id = this.user_Id;
                        obj.AddName = this.addName;
                        obj.AddTime = this.addTime;
                        obj.UpdName = '呓语';
                        obj.UpdTime = year;
                        layui.form.render(); // 重新渲染表单元素，确保 Layui 应用新的选中状态
                        //发送AJAX请求
                        $.ajax({
                            url: '/Client/GetCusContactShow',
                            type: 'get',
                            data: showcuscontact,
                            dataType: 'json',
                            success: function (res) {
                                var data = JSON.parse(res)
                                var table = layui.table;
                                console.log(data)
                                // 创建渲染实例
                                var tableIns = table.render({
                                    elem: '#test',
                                    cols: [[
                                        {
                                            type: 'checkbox', fixed: 'left'
                                        },  // 复选框列
                                        {
                                            field: 'ccontact', title: '联系人', width: 200,
                                            templet: function (d) {
                                                return d.ccontact || '<input type="text" id="ccontacts" class="layui-input">'; // 使用文本框显示联系人
                                            }
                                        },
                                        {
                                            field: 'caddress', title: '地址', width: 200,
                                            templet: function (d) {
                                                return d.caddress || '<input type="text" id="caddresss" class="layui-input">'; // 使用文本框显示联系人
                                            }
                                        },
                                        {
                                            field: 'cphone', title: '电话', width: 200,
                                            templet: function (d) {
                                                return d.cphone || '<input type="text" id="cphones" class="layui-input">'; // 使用文本框显示联系人
                                            }
                                        },
                                        {
                                            field: 'creatTime', title: '创建时间', width: 200, templet: function (d) {
                                                let dateobj = new Date(d.creatTime);
                                                let dateyear = dateobj.getFullYear();
                                                let datemonth = ('0' + (dateobj.getMonth() + 1)).slice(-2);
                                                let dateday = ('0' + dateobj.getDate()).slice(-2);
                                                let datehours = ('0' + (dateobj.getHours())).slice(-2);
                                                let dateminutes = ('0' + (dateobj.getMinutes())).slice(-2);
                                                let dateseconds = ('0' + (dateobj.getSeconds())).slice(-2);
                                                return dateyear + '-' + datemonth + '-' + dateday + ' ' + datehours + ':' + dateminutes + ':' + dateseconds;
                                            }
                                        },
                                        {
                                            title: '操作',
                                            templet: function (d) {
                                                if (d.isNew) {
                                                    // 新增行显示“保存”按钮
                                                    return ' <button type="button" onclick="saveNewItem(' + d.cusContactId + ')" class="layui-btn layui-btn-xs layui-btn-normal">保存</button>';
                                                } else {
                                                    // 普通行显示“编辑”和“删除”按钮
                                                    return ' <button type="button" onclick="deleteItem(' + d.cusContactId + ')" class="layui-btn layui-btn-xs">删除</button>'
                                                        //+ ' <button type="button" onclick="editItem(' + d.cusContactId + ')" class="layui-btn layui-btn-xs">编辑</button>'
                                                        ;
                                                }
                                            }
                                        },
                                    ]],
                                    done: function (res, curr, count) {
                                        // 假设返回的数据包含了 selectedIds，表示选中的行的 ID 数组
                                        var selectedIds = [obj.CusContactId];  // 这里是你根据业务逻辑设置的需要选中的行的 ID
                                        console.log(selectedIds)
                                        // 遍历所有数据
                                        res.data.forEach(function (item) {
                                            // 判断该行的 ID 是否在 selectedIds 中
                                            if (selectedIds.indexOf(item.cusContactId) !== -1) {
                                                // 选中复选框
                                                console.log(selectedIds)
                                                console.log(item.cusContactId)
                                                var checkbox = $('input[type="checkbox"][name="layui-table-checkbox"][value="' + item.cusContactId + '"]');
                                                checkbox.prop('checked', true);  // 通过 value 属性手动选中复选框
                                            }
                                        });
                                        // 重新渲染复选框状态（如果需要的话）
                                        form.render('checkbox');
                                    },
                                    data: data.data
                                })


                                // 新增行按钮的点击事件
                                $('#addRowBtn').on('click', function () {
                                    // 模拟新增一行数据
                                    var newRow = {
                                        //cusContactId: data.data.length + 1, // 新行的 ID
                                        ccontact: '', // 默认联系人
                                        caddress: '', // 默认地址
                                        cphone: '', // 默认电话
                                        creatTime: year, // 当前时间
                                        isNew: true // 标记为新增行
                                    };
                                    // 将新行添加到现有数据中
                                    data.data.push(newRow);

                                    // 重新渲染表格
                                    table.reload('test', {
                                        data: data.data // 更新数据
                                    });
                                });
                                // AJAX请求和表格渲染代码

                                table.on('checkbox(testFilter)', function (obj) {
                                    console.log(obj);
                                    // 当前选中的行加入到 selectedRows 数组中
                                    if (obj.checked) {
                                        selectedRows.push(obj.data.cusContactId);
                                    }
                                    else {
                                        // 当前取消选中的行从 selectedRows 数组中移除
                                        selectedRows = selectedRows.filter(row => row.cusContactId !== obj.data.cusContactId);
                                    }
                                    console.log(selectedRows)
                                });
                            }
                        })
                    })
                }
            })
            
            

            layui.use(function () {
                var $ = layui.$;
                var layer = layui.layer;
                var util = layui.util;
                var form = layui.form;
                // 事件
                layer.open({
                    type: 1, // page 层类型
                    area: ['1000px', '700px'], // 宽高
                    title: '修改单位',
                    content: `<div class="layui-form" lay-filter="filter-test-layer" style="margin: 16px;">
                       <table>
                             <tr>
                            <td>
                                   <div class="layui-form-item" style="float:left">
                                      <div class="layui-input-group">
                                         <div class="layui-input-split layui-input-prefix">
                                             客户编号：
                                                 </div>
                                                <input type="text" id="UpdClientCode" name="ClientCode" placeholder="请输入" class="layui-input">
                                           </div>
                                       </div>
                                     </td>
                                           <td>
                                       <div class="layui-form-item">
                                            <div class="layui-input-group">
                                                <div class="layui-input-split layui-input-prefix">
                                                 客户名称：
                                                 </div>
                                                <input type="text" lay-verify="onlyNumbers" id="UpdClientName" name="ClientName" placeholder="请输入" class="layui-input">
                                                </div>
                                            </div>
                                             </td>
                                      </tr>
                                          <tr>
                                          <td>
                                                <div class="layui-form-item" style="float:left">
                                      <div class="layui-input-group">
                                         <div class="layui-input-split layui-input-prefix">
                                             传真：
                                          </div>
                                                <input type="text" id="UpdCfax" name="Cfax" placeholder="请输入" class="layui-input">
                                           </div>
                                       </div>
                                      </td>
                                       <td>
                                           <div class="layui-form-item" style="float:left">
                                      <div class="layui-input-group">
                                         <div class="layui-input-split layui-input-prefix">
                                             电话：
                                          </div>
                                               <input type="text" id="UpdLandline" name="Landline" placeholder="请输入" class="layui-input">
                                           </div>
                                       </div>
                                                   </td>
                                               </tr>
                                              <tr>
                                           <td>
                                             <div class="layui-form-item" style="float:left">
                                      <div class="layui-input-group">
                                         <div class="layui-input-split layui-input-prefix">
                                             邮箱：
                                                 </div>
                                               <input type="text" id="UpdMailbox" name="Mailbox" placeholder="请输入" class="layui-input">
                                           </div>
                                       </div>
                                           </td>
                                        <td>
                                           <div class="layui-form-item" style="float:left">
                                      <div class="layui-input-group">
                                         <div class="layui-input-split layui-input-prefix">
                                             备注：
                                                </div>
                                                <input type="text" id="UpdCremark" name="Cremark" placeholder="请输入" class="layui-input">
                                           </div>
                                                    </div>
                                                   </td>
                                               </tr>
                                           </table>
                                     <div class="layui-form-item">
                                         <button class="layui-btn layui-btn-fluid" style="width:200px;float:right" lay-submit lay-filter="demo-login">提交</button>
                                     </div>
                        </div>
                         <div style="padding: 16px;">
                            <button id="addRowBtn" class="layui-btn layui-btn-normal">新增一行</button>
                            <table class="layui-hide" id = "test" lay-filter="testFilter"></table > 
                         </div> 
                        </tbody>
                        </table>
                        </form> `,
                    success: function () {
                        var obj1 = {
                            ClientId: '',
                            ClientCode: '',
                            ClientName: '',
                            Cfax: '',
                            Landline: '',
                            Mailbox: '',
                            Cremark: '',
                            CusContactId: '',
                            Office_Id: '',
                            User_Id: '',
                            AddName: '',
                            AddTime: '',
                            UpdName: '呓语',
                            UpdTime: year,
                            Token: token
                        };
                        form.render();
                        
                        //表单提交
                        form.on('submit(demo-login)', function (data) {
                            var field = data.field;//获取表单字段值
                            obj1.ClientId = obj.ClientId;
                            obj1.ClientCode = field.ClientCode;
                            obj1.ClientName = field.ClientName;
                            obj1.Cfax = field.Cfax;
                            obj1.Landline = field.Landline;
                            obj1.Mailbox = field.Mailbox;
                            obj1.Cremark = field.Cremark;
                            obj1.CusContactId = selectedRows;
                            obj1.Office_Id = obj.Office_Id;
                            obj1.User_Id = obj.User_Id;
                            obj1.AddName = obj.addName;
                            obj1.AddTime = obj.addTime;
                            obj1.UpdName = '呓语';
                            obj1.UpdTime = year;
                            obj1.Token = token;
                            $.ajax({
                                url: '/Client/UpdClient',
                                data: { uclient: obj1 },
                                type: 'put',
                                success: function (res) {
                                    var s = JSON.parse(res)
                                    if (s.data > 0) {
                                        layer.msg('修改成功', { icon: 1 }, function () {
                                            location.reload()
                                        });
                                    }
                                    else {
                                        layer.msg('修改失败');
                                        return
                                    }
                                }
                            })
                            return false;
                        })
                    }
                });
            });
        };
        /////删除//////
        window.deleteItem = function (clientId) {
            var layer = layui.layer;
            layer.confirm('你确定要删除这条数据吗？', {
                title: '提示'
            }, function (index) {
                let delclient = {};
                delclient.ClientId = clientId;
                delclient.DelName = "呓语";
                delclient.DelTime = year;
                delclient.Token = token;
                //执行删除操作
                $.ajax({
                    url: '/Client/DelAllClient',
                    type: 'post',
                    data: delclient,
                    success: function (res) {
                        var s = JSON.parse(res)
                        if (s.data > 0) {
                            layer.msg('删除成功', { icon: 1 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('删除失败');
                            return
                        }
                    }
                })
            });
        };
    });
}
//添加客户弹出层
layui.use(['layer'], function () {
    var layer = layui.layer;
    document.getElementById('openLayerBtn').onclick = function () {
        layer.open({
            type: 1,
            title: '添加与列表展示',
            area: ['1300px', '800px'], // 宽高,
            content: '<br/><div id="layer-content"></div>'
        });
        // 填充弹出层内容
        fillLayerContent();
    };
});
function fillLayerContent() {
    var idss = [];
    var cheddatas = [];
    var form = layui.form
    var table = layui.table;
    // 获取弹出层内容的DOM元素
    var layerContent = document.getElementById('layer-content');
    // 构建表单部分
    var formHtml = ' <div class="layui-form" lay - filter="filter-test-layer" style = "margin: 16px;" >' +
        '<table>' +
        '<tr>' +
        '<td>' +
        '<div class="layui-form-item">' +
        '<div class="layui-input-group">' +
        '<div class="layui-input-split layui-input-prefix">' +
        ' 客户编号：' +
        ' </div>' +
        '<input type="text" lay-verify="onlyNumbers" id="ClientCodes" name="ClientCode" placeholder="请输入" class="layui-input">' +
        '</div>' +
        '</div>' +
        '</td>' +
        '<td>' +
        '<div class="layui-form-item">' +
        '<div class="layui-input-group">' +
        '<div class="layui-input-split layui-input-prefix">' +
        ' 客户名称：' +
        ' </div>' +
        '<input type="text" lay-verify="onlyNumbers" id="ClientName" name="ClientName" placeholder="请输入" class="layui-input">' +
        '</div>' +
        '</div>' +
        '</td>' +
        '</tr>' +

        '<tr>' +
        '<td>' +
        '<div class="layui-form-item">' +
        '<div class="layui-input-group">' +
        '<div class="layui-input-split layui-input-prefix">' +
        ' 传真：' +
        ' </div>' +
        '<input type="text" lay-verify="onlyNumbers" id="Cfax" name="Cfax" placeholder="请输入" class="layui-input">' +
        '</div>' +
        '</div>' +
        '</td>' +
        '<td>' +
        '<div class="layui-form-item">' +
        '<div class="layui-input-group">' +
        '<div class="layui-input-split layui-input-prefix">' +
        ' 电话：' +
        ' </div>' +
        '<input type="text" lay-verify="onlyNumbers" id="Landline" name="Landline" placeholder="请输入" class="layui-input">' +
        '</div>' +
        '</div>' +
        '</td>' +
        '</tr>' +

        '<tr>' +
        '<td>' +
        '<div class="layui-form-item">' +
        '<div class="layui-input-group">' +
        '<div class="layui-input-split layui-input-prefix">' +
        ' 邮箱：' +
        ' </div>' +
        '<input type="text" lay-verify="onlyNumbers" id="Mailbox" name="Mailbox" placeholder="请输入" class="layui-input">' +
        '</div>' +
        '</div>' +
        '</td>' +
        '<td>' +
        '<div class="layui-form-item">' +
        '<div class="layui-input-group">' +
        '<div class="layui-input-split layui-input-prefix">' +
        ' 备注：' +
        ' </div>' +
        '<input type="text" lay-verify="onlyNumbers" id="Cremark" name="Cremark" placeholder="请输入" class="layui-input">' +
        '</div>' +
        '</div>' +
        '</td>' +
        '</tr>' +
        '</table>' +
        '<div style="padding: 16px;">' +
        '<button id="addRowBtn" class="layui-btn layui-btn-normal">新增一行</button>' +
        '<table class="layui-hide" id = "test" lay-filter="testFilter"></table > ' +
        '</div> ' +
        '<button class="layui-btn layui-btn-primary layui-border-blue" id="tccqx" style="width:200px">取消</button>' +
        '<button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login" style="width:200px;float:right">提交</button>' +
        '</div>' +
        '</div>' +
        '</div>';

    // 将表单和列表HTML添加到弹出层内容容器中
    layerContent.innerHTML = formHtml;
   
    layui.use(['table'], function () {
        var table = layui.table;
        let showcuscontact = {
            Token: token
        };
        //发送AJAX请求
        $.ajax({
            url: '/Client/GetCusContactShow',
            type: 'get',
            data: showcuscontact,
            dataType: 'json',
            success: function (res) {
                var data = JSON.parse(res)

                console.log(data)
                // 创建渲染实例
                table.render({
                    elem: '#test',
                    cols: [[
                        { type: 'checkbox' },
                        {
                            field: 'ccontact', title: '联系人', width: 200,
                            templet: function (d) {
                                return d.ccontact || '<input type="text" id="ccontacts" class="layui-input">'; // 使用文本框显示联系人
                            }
                        },
                        {
                            field: 'caddress', title: '地址', width: 200,
                            templet: function (d) {
                                return d.caddress || '<input type="text" id="caddresss" class="layui-input">'; // 使用文本框显示联系人
                            }
                        },
                        {
                            field: 'cphone', title: '电话', width: 200,
                            templet: function (d) {
                                return d.cphone || '<input type="text" id="cphones" class="layui-input">'; // 使用文本框显示联系人
                            }
                        },
                        {
                            field: 'creatTime', title: '创建时间', width: 200, templet: function (d) {
                                let dateobj = new Date(d.creatTime);
                                let dateyear = dateobj.getFullYear();
                                let datemonth = ('0' + (dateobj.getMonth() + 1)).slice(-2);
                                let dateday = ('0' + dateobj.getDate()).slice(-2);
                                let datehours = ('0' + (dateobj.getHours())).slice(-2);
                                let dateminutes = ('0' + (dateobj.getMinutes())).slice(-2);
                                let dateseconds = ('0' + (dateobj.getSeconds())).slice(-2);
                                return dateyear + '-' + datemonth + '-' + dateday + ' ' + datehours + ':' + dateminutes + ':' + dateseconds;
                            }
                        },
                        {
                            title: '操作',
                            templet: function (d) {
                                if (d.isNew) {
                                    // 新增行显示“保存”按钮
                                    return ' <button type="button" onclick="saveNewItem(' + d.cusContactId + ')" class="layui-btn layui-btn-xs layui-btn-normal">保存</button>';
                                } else {
                                    // 普通行显示“编辑”和“删除”按钮
                                    return ' <button type="button" onclick="deleteItem(' + d.cusContactId + ')" class="layui-btn layui-btn-xs">删除</button>'
                                        //+ ' <button type="button" onclick="editItem(' + d.cusContactId + ')" class="layui-btn layui-btn-xs">编辑</button>'
                                        ;
                                }
                            }
                        },
                    ]],
                    data: data.data
                })
                // 新增行按钮的点击事件
                $('#addRowBtn').on('click', function () {
                    // 模拟新增一行数据
                    var newRow = {
                        //cusContactId: data.data.length + 1, // 新行的 ID
                        ccontact: '', // 默认联系人
                        caddress: '', // 默认地址
                        cphone: '', // 默认电话
                        creatTime: year, // 当前时间
                        isNew: true // 标记为新增行
                    };
                    // 将新行添加到现有数据中
                    data.data.push(newRow);

                    // 重新渲染表格
                    table.reload('test', {
                        data: data.data // 更新数据
                    });
                });
            }
        })
        // AJAX请求和表格渲染代码
        let selectedRows = [];
        table.on('checkbox(testFilter)', function (obj) {
            console.log(obj);
            // 当前选中的行加入到 selectedRows 数组中
            if (obj.checked) {
                selectedRows.push(obj.data.cusContactId);
            }
            else {
                // 当前取消选中的行从 selectedRows 数组中移除
                selectedRows = selectedRows.filter(row => row.cusContactId !== obj.data.cusContactId);
            }
            console.log(selectedRows)
        });
        //点击提交添加
        var code = Math.floor(Math.random() * 1000000) + 100000;
        $("#ClientCodes").val(code)
        form.on('submit(demo-login)', function (data) {
            // 渲染Layui表单（确保表单验证等功能可用）
            var field = data.field;//获取表单字段值
            
            layui.use(['form'], function () {
                var selectedRowsss = JSON.stringify(selectedRows)
                console.log(selectedRowsss)
                var form = layui.form;
                form.render();
                //表单提交
                let obj1 = {
                    ClientCode: field.ClientCode,
                    ClientName: field.ClientName,
                    Cfax: field.Cfax,
                    Landline: field.Landline,
                    Mailbox: field.Mailbox,
                    Cremark: field.Cremark,
                    CusContactId: selectedRows,
                    Office_Id: '18591113953464852',
                    User_Id: '3',
                    AddName: '呓语',
                    AddTime: year,
                    UpdName: null,
                    UpdTime: null,
                    Token: token
                };
                $.ajax({
                    url: '/Client/AddClients',
                    data: { obj: obj1 },
                    type: 'post',
                    success: function (res) {
                        var s = JSON.parse(res)
                        if (s.data > 0) {
                            layer.msg('添加成功', { icon: 1 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('添加失败');
                            return;
                        }
                    }
                });
            })
        });
    });
    // 保存新增的行
    window.saveNewItem = function (id) {
        // 在这里处理保存新增数据的逻辑
        layui.use(['form'], function () {
            var form = layui.form;
            form.render();
            //表单提交
            let obj1 = {
                Ccontact: $("#ccontacts").val(),
                Caddress: $("#caddresss").val(),
                Cphone: $("#cphones").val(),
                CreatTime: year,
                Office_Id: '18591113953464852',
                User_Id: '3',
                AddName: '呓语',
                AddTime: year,
                UpdName: null,
                UpdTime: null,
                DelName: null,
                DelTime: null,
                Token: token
            };
            $.ajax({
                url: '/Client/AddCusContacts',
                data: { obj: obj1 },
                type: 'post',
                success: function (res) {
                    var s = JSON.parse(res)
                    if (s.data > 0) {
                        layer.msg('添加成功', { icon: 1 }, function () {
                            location.reload()
                        });
                    }
                    else {
                        layer.msg('添加失败');
                        return;
                    }
                }
            });
        })
    };
    //删除
    window.deleteItem = function (cusContactId) {
        var layer = layui.layer;
        layer.confirm('你确定要删除这条数据吗？', {
            title: '提示'
        }, function (index) {
            let delcuscontact = {};
            delcuscontact.CusContactId = cusContactId;
            delcuscontact.DelName = "呓语";
            delcuscontact.DelTime = year;
            delcuscontact.Token = token;
            //执行删除操作
            $.ajax({
                url: '/Client/DelAllCusContact',
                type: 'put',
                data: delcuscontact,
                success: function (res) {
                    var s = JSON.parse(res)
                    if (s.data > 0) {
                        layer.msg('删除成功', { icon: 1 }, function () {
                            location.reload()
                        });
                    }
                    else {
                        layer.msg('删除失败');
                        return
                    }
                }
            })
        });
    };
    //弹窗取消
    $("#tccqx").click(function () {
        location.reload();
    })
}

function QX() {
    location.reload();
}
